package lib_model.Transportion;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import lib_dal.Connection.ConnectMSSQL;
import lib_dal.Interface.ISql;
import lib_model.Orther.ReturnObj;

public class Transportion_type implements ISql {

	public String name;
	public String description;
	public Date date_create;

	public Transportion_type() {
	}

	public Transportion_type(String name) {
		super();
		this.name = name;
	}

	@Override
	public ReturnObj Select() {
		String sql = "select name, description, date_create from dbo.transportion_type";
		ReturnObj obj = new ReturnObj();
		ArrayList<Transportion_type> arr = new ArrayList<Transportion_type>();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Transportion_type a = new Transportion_type();
				a.name = rs.getString("name");
				a.description = rs.getString("description");
				a.date_create = rs.getDate("date_create");
				arr.add(a);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		obj.ObjResult = arr;
		return obj;
	}

	@Override
	public ReturnObj Add() {
		String sql = "insert into dbo.transportion_type (name,   description,   date_create) values   (?,   ?,   GETDATE())";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.name);
			preState.setString(2, this.description);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Update() {
		String sql = "update dbo.transportion_type set description = ?,   date_create = ? where  name = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.description);
			preState.setDate(2, (java.sql.Date) this.date_create);
			preState.setString(3, this.name);
			obj.Result = (preState.executeUpdate() != 0) ? true : false;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj Delete() {
		String sql = "delete from dbo.transportion_type where name = ?";
		ReturnObj obj = new ReturnObj();
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.name);
			obj.Result = preState.execute();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

	@Override
	public ReturnObj GetInformation() {
		ReturnObj obj = new ReturnObj();
		String sql = "select name, description, date_create from dbo.transportion_type where name = ?";
		try {
			PreparedStatement preState = new ConnectMSSQL()
					.preparedStatement(sql);
			preState.setString(1, this.name);
			ResultSet rs = preState.executeQuery();
			while (rs.next()) {
				Transportion_type a = new Transportion_type();
				a.name = rs.getString("name");
				a.description = rs.getString("description");
				a.date_create = rs.getDate("date_create");
				obj.ObjResult = a;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return obj;
	}

}
